package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.*;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.category.CategoryDTO;
import io.github.talelin.latticy.dto.category.UpdateCategoryDTO;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.model.CategoryDO;
import io.github.talelin.latticy.service.CategoryService;
import io.github.talelin.latticy.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;
import java.util.List;

/**
 * ClassName CategoryController
 * Description
 * Create by zcy
 * Date 2022-06-09
 */
@RestController
@RequestMapping("/v1/category")
@Validated
@PermissionModule(value = "品类管理")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;


//    @GetMapping("/page")
//    @PermissionMeta(value = "品类分页查询")
//    @GroupRequired
//    public PageResponseVO<CategoryDO> getCategoryPage(@Validated CategoryPageDTO categoryPageDTO) {
//        return PageUtil.build(categoryService.getCategoryPage(categoryPageDTO));
//    }

    @GetMapping("/page")
    public PageResponseVO<CategoryDO> page(
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam @Positive(message = "{id.positive}") Integer root
    ) {
        return PageUtil.build(categoryService.getCategoryPage(count, page, root));
    }

    @GetMapping("/sub-page")
    public PageResponseVO<CategoryDO> subPage(@Validated BasePageDTO dto, @RequestParam(name = "id") @Positive(message = "{id}") Integer id) {
        IPage<CategoryDO> paging = categoryService.getSubCategoriesByPage(dto.getCount(), dto.getPage(), id);
        return PageUtil.build(paging);
    }

    @PostMapping("")
    @GroupRequired
    @PermissionMeta(value = "创建品类")
    @Logger(template = "{user.username}创建品类:{request.category_name}")
    public CreatedVO createCategory(@RequestBody @Validated CategoryDTO categoryDTO) {
        categoryService.createCategory(categoryDTO);
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "编辑品类")
    @Logger(template = "{user.username}修改品类:{request.categoryMapper-categoryName}")
    public UpdatedVO updateCategory(@PathVariable @Positive(message = "{category.id}") Integer id, @RequestBody @Validated UpdateCategoryDTO categoryDTO) {
        categoryService.updateCategory(id, categoryDTO);
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    @GroupRequired
    @PermissionMeta(value = "删除品类")
    @Logger(template = "{user.username}删除品类:{request.categoryMapper-categoryName}")
    public DeletedVO deleteCategory(@PathVariable @Positive(message = "{category.id}") Integer id) {
        categoryService.deleteCategory(id);
        return new DeletedVO();
    }

    @GetMapping("/getCategoryTree")
    @GroupRequired
    @PermissionMeta(value = "品类列表")
    public FindResultVO getCategoryTree() {
        return FindResultVO.success(categoryService.getCategoryTree());
    }

    @GetMapping("/{id}")
    @LoginRequired
    public FindResultVO getById(@PathVariable Integer id) {
        return FindResultVO.success(categoryService.getById(id));
    }


    @PostMapping("/getByIds")
    public FindResultVO getByIds(@RequestBody List<Integer> ids) {
        return FindResultVO.success(categoryService.listByIds(ids));
    }

    @GetMapping("/findParentId/{id}")
    public FindResultVO findParentId(@PathVariable Integer id) {
        return FindResultVO.success(categoryService.findParentId(id));
    }
}
